﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LTorrent.Torrent.File
{
    #region BList
    /// <summary>
    /// List
    /// start - 'l'
    /// end - 'e'
    /// Example - l5:helloi145e => ("hello",145)
    /// </summary>
    public class BList
    {
        List<BElement> Items = null;

        public BList()
        {
            Items = new List<BElement>();
        }

        public BElement this[int index]
        {
            get
            {
                if (Items.Count > index)
                {

                    return Items[index];
                }
                return new BElement();
            }
            set
            {
                if (Items.Count > index)
                {
                    Items[index] = value;
                }
                else
                {
                    Items.Add(value);
                }
            }
        }
        public int Count
        {
            get
            {
                return Items.Count;
            }
        }

        /// <summary>
        /// Adds new item to dictionary
        /// </summary>
        /// <param name="First">First Item</param>
        /// <param name="Second">Second Item</param>
        public void Add(BElement inf)
        {
            Items.Add(inf);
        }
      
        public void Serialize(BinaryWriter outStream)
        {
            outStream.Write('l');
            foreach (var X in Items)
            {
                X.Serialize(outStream);
            }
            outStream.Write('e');
        }

    }
    #endregion
}
